package com.dts.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebFilter(filterName = "loginFilter",urlPatterns = "/*",dispatcherTypes = DispatcherType.REQUEST)
public class LoginFilter implements Filter {

    public void doFilter(ServletRequest request1, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) request1;
        HttpServletResponse res = (HttpServletResponse) response;
        System.out.println("url:" + request.getRequestURI());


        if (request.getSession().getAttribute("user")!=null
                ||request.getRequestURI().contains("login")
                ||request.getRequestURI().contains("register")
        ){
            chain.doFilter(request, response);
        }else {
            res.sendRedirect("login.jsp");
        }
    }
}
